package repository

import (
	"context"
	"intelli-dl-online-logo-go/utils"
	"time"
)

type VerifyRepository interface {
	StoreSMSCode(ctx context.Context, phone, code string) error
	StoreEmailCode(ctx context.Context, email, code string) error
	GetSMSCode(ctx context.Context, phone string) (string, error)
	GetEmailCode(ctx context.Context, email string) (string, error)
}

type verifyRepositoryImpl struct {
}

func NewVerifyRepository() VerifyRepository {
	return &verifyRepositoryImpl{}
}

func (vr *verifyRepositoryImpl) StoreSMSCode(ctx context.Context, phone, code string) error {
	return utils.RedisClient.Set(ctx, utils.SMS_CODE_PREFIX+phone, code, utils.SMS_CODE_EXPIRE*time.Second).Err()
}

func (vr *verifyRepositoryImpl) StoreEmailCode(ctx context.Context, email, code string) error {
	return utils.RedisClient.Set(ctx, utils.EMAIL_CODE_PREFIX+email, code, utils.EMAIL_CODE_EXPIRE*time.Second).Err()
}

func (vr *verifyRepositoryImpl) GetSMSCode(ctx context.Context, phone string) (string, error) {
	return utils.RedisClient.Get(ctx, utils.SMS_CODE_PREFIX+phone).Result()
}

func (vr *verifyRepositoryImpl) GetEmailCode(ctx context.Context, email string) (string, error) {
	return utils.RedisClient.Get(ctx, utils.EMAIL_CODE_PREFIX+email).Result()
}
